package org.htmlparser.parserapplications.filterbuilder.wrappers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JComboBox;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.parserapplications.filterbuilder.Filter;
import org.htmlparser.tags.CompositeTag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class TagNameFilterWrapper extends Filter
  implements ActionListener
{
  protected TagNameFilter mFilter = new TagNameFilter();
  protected JComboBox mName = new JComboBox();

  public TagNameFilterWrapper()
  {
    this.mName.setEditable(true);
    add(this.mName);
    this.mName.addItem(this.mFilter.getName());
    this.mName.addActionListener(this);
  }

  public boolean accept(Node paramNode)
  {
    return this.mFilter.accept(paramNode);
  }

  public void actionPerformed(ActionEvent paramActionEvent)
  {
    if (paramActionEvent.getSource() == this.mName)
    {
      Object[] arrayOfObject = this.mName.getSelectedObjects();
      if ((arrayOfObject != null) && (arrayOfObject.length != 0))
        this.mFilter.setName((String)arrayOfObject[0]);
    }
  }

  protected void addName(Set paramSet, Node paramNode)
  {
    if (paramNode instanceof Tag)
    {
      paramSet.add(((Tag)paramNode).getTagName());
      if (paramNode instanceof CompositeTag)
      {
        NodeList localNodeList = ((CompositeTag)paramNode).getChildren();
        if (localNodeList != null)
          for (int i = 0; i < localNodeList.size(); ++i)
            addName(paramSet, localNodeList.elementAt(i));
      }
    }
  }

  public String getDescription()
  {
    return "Tag named";
  }

  public String getIconSpec()
  {
    return "images/TagNameFilter.gif";
  }

  public NodeFilter getNodeFilter()
  {
    TagNameFilter localTagNameFilter = new TagNameFilter();
    localTagNameFilter.setName(this.mFilter.getName());
    return localTagNameFilter;
  }

  public NodeFilter[] getSubNodeFilters()
  {
    return new NodeFilter[0];
  }

  public void setNodeFilter(NodeFilter paramNodeFilter, Parser paramParser)
  {
    this.mFilter = ((TagNameFilter)paramNodeFilter);
    HashSet localHashSet = new HashSet();
    paramParser.reset();
    try
    {
      NodeIterator localNodeIterator = paramParser.elements();
      if (localNodeIterator.hasMoreNodes())
        addName(localHashSet, localNodeIterator.nextNode());
    }
    catch (ParserException localParserException)
    {
      Iterator localIterator = localHashSet.iterator();
      while (localIterator.hasNext())
        this.mName.addItem(localIterator.next());
      this.mName.setSelectedItem(this.mFilter.getName());
    }
  }

  public void setSubNodeFilters(NodeFilter[] paramArrayOfNodeFilter)
  {
  }

  public String toJavaCode(StringBuffer paramStringBuffer, int[] paramArrayOfInt)
  {
    StringBuffer localStringBuffer = new StringBuffer().append("filter");
    int i = paramArrayOfInt[1];
    paramArrayOfInt[1] = (i + 1);
    String str = i;
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append("TagNameFilter ");
    paramStringBuffer.append(str);
    paramStringBuffer.append(" = new TagNameFilter ();");
    newline(paramStringBuffer);
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append(str);
    paramStringBuffer.append(".setName (\"");
    paramStringBuffer.append(this.mFilter.getName());
    paramStringBuffer.append("\");");
    newline(paramStringBuffer);
    return str;
  }
}